﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class VIPComputeUtil {

    /// <summary>
    /// 计算滑动方向（x为正值则向左移动，y为正值则向下移动，反之则反方向）
    /// </summary>
    /// <param name="startPos">起始坐标(二维)</param>
    /// <param name="endPos">终点坐标(二维)</param>
    /// <param name="deviation">误差值</param>
    /// <returns>二维向量</returns>
    public static Vector2 ComputeSlipDirection(Vector2 startPos,Vector2 endPos,float deviation = 0.01f) {
        Vector2 newV2 = new Vector2();
        Vector2 startV2 = startPos.normalized;
        Vector2 endV2 = endPos.normalized;
        if (Mathf.Abs(startV2.x - endV2.x) < deviation && Mathf.Abs(startV2.y - endV2.y) < deviation)
        {
            return newV2;
        }
        newV2.x = startPos.x - endPos.x;
        newV2.y = startPos.y - endPos.y;
        return newV2.normalized;
    }

    /// <summary>
    /// 计算出日期相差的天数
    /// </summary>
    /// <param name="startDate"></param>
    /// <param name="endDate"></param>
    /// <returns></returns>
    public static int ComputeDifferDay(DateTime startDate, DateTime endDate) {
        TimeSpan ts = endDate - startDate;
        return ts.Days;
    }

    /// <summary>
    /// 计算出日期相差的天数
    /// </summary>
    /// <param name="startDate">格式：1996-2-13</param>
    /// <param name="endDate">格式：1996-2-13</param>
    /// <returns></returns>
    public static int ComputeDifferDay(string startDate, string endDate) {
        DateTime endDt = VIPConvertUtil.StringToDateTime(endDate);
        DateTime startDt = VIPConvertUtil.StringToDateTime(startDate);
        TimeSpan ts = endDt - startDt;
        return ts.Days;
    }

    /// <summary>
    /// 获取鼠标的位置
    /// </summary>
    /// <param name="canvas"></param>
    /// <param name="rectTransform"></param>
    /// <returns></returns>
    public static Vector2 MousePosition(Canvas canvas) {
        Vector2 pos;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, 
            Input.mousePosition, canvas.worldCamera, out pos))
        {
            return pos;
        }
        return Vector2.zero;
    }

    /// <summary>
    /// 两点之间求中心点(平面)
    /// </summary>
    /// <param name="startV2"></param>
    /// <param name="endV2"></param>
    /// <returns></returns>
    public static Vector2 CenterPoint(Vector2 startV2, Vector2 endV2)
    {
        Vector2 centerPoint = Vector2.zero;
        centerPoint.x = (startV2.x + endV2.x) / 2;
        centerPoint.y = (startV2.y + endV2.y) / 2;
        return centerPoint;
    }

    /// <summary>
    /// 两点之间求中心点(空间)
    /// </summary>
    /// <param name="startV3"></param>
    /// <param name="endV3"></param>
    /// <returns></returns>
    public static Vector3 CenterPoint(Vector3 startV3, Vector3 endV3)
    {
        Vector3 centerPoint = Vector3.zero;
        centerPoint.x = (startV3.x + endV3.x) / 2;
        centerPoint.y = (startV3.y + endV3.y) / 2;
        centerPoint.z = (startV3.z + endV3.z) / 2;
        return centerPoint;
    }

    /// <summary>
    /// 两点之间求中心点(四维)
    /// </summary>
    /// <param name="startV4"></param>
    /// <param name="endV4"></param>
    /// <returns></returns>
    public static Vector4 CenterPoint(Vector4 startV4, Vector4 endV4) {
        Vector4 centerPoint = Vector4.zero;
        centerPoint.x = (startV4.x + endV4.x) / 2;
        centerPoint.y = (startV4.y + endV4.y) / 2;
        centerPoint.z = (startV4.z + endV4.z) / 2;
        centerPoint.w = (startV4.w + endV4.w) / 2;
        return centerPoint;
    }

}
